/*
 * mox_slave_xc.xc
 *
 *  Created on: Jul 11, 2010
 *      Author: jmonk
 */
#include <xs1.h>
#include <print.h>
#include "mox.h"
#include "mox_slave.h"
#include "mox_storage.h"
#include "mox_module.h"
#include "mox_threads.h"

void slave_snd_rcv(streaming chanend mod[], unsigned mod_n, unsigned mod_msg, streaming chanend thd[], unsigned thd_n, unsigned thd_msg, unsigned state)
{
	int i;
	unsigned t;
	unsigned tmp;
	timer tmr;
	for (i = 0; i < mod_n; i++) {
		mod[i] <: mod_msg;
	}
	for (i = 0; i < thd_n; i++) {
		thd[i] <: thd_msg;
	}
	tmp = 1;
	tmr :> t;
	while (tmp) {
		select {
		case tmr when timerafter(t + SLAVE_WAIT_DELAY/4) :> void:
			tmp = 0;
			break;
		default:
			for (i = 0; i < mod_n; i++) {
				select {
				case mod[i] :> tmp:
					slave_handle_mod(state, i, tmp);
					break;
				default:
					break;
				}
			}
			for (i = 0; i < thd_n; i++) {
				select {
				case thd[i] :> tmp:
					slave_handle_thd(state, i, tmp);
					break;
				default:
					break;
				}
			}
			tmp = 1;
			break;
		}
	}

	return;
}
